﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Core;
using Core.Models;
using System.Reflection;

namespace ContactForm.Controllers
{
    [Permission("Can Administer BU")]
    public class AdminController : Controller
    {
        //
        // GET: /Admin/

        public ActionResult Index()
        {
            Guid BUId = (Guid)Session["BusinessUnitId"];
            ViewBag.BusinessUnitId = BUId;
            return View();
        }

        //
        // POST: /List/

        [HttpPost]
        public JsonResult MsgList(FormCollection collection)
        {
            CoreEntities core = new CoreEntities();
            Guid BUId = new Guid(collection.GetValues("BusinessUnitId")[0].ToString());
            int SkipRecs = int.Parse(collection.GetValues("iDisplayStart")[0].ToString());
            int TakeCount = int.Parse(collection.GetValues("iDisplayLength")[0].ToString());
            string strSearch = collection.GetValues("sSearch")[0].ToString();
            DataTableResponse r = new DataTableResponse();
            // Needs update when paging & sorting is implemented
            Guid PluginId = new Guid(((System.Runtime.InteropServices.GuidAttribute)(Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(System.Runtime.InteropServices.GuidAttribute), false)[0])).Value);
            List<Core.Models.Message> list = core.Messages.Where(m => (m.BusinessUnitId == BUId && m.PluginId == PluginId && m.Subject.Contains(strSearch) && m.IsDroppedYN == "N")).OrderByDescending(m => m.CreateDT).Skip(SkipRecs).Take(TakeCount).ToList();
            r.sEcho = collection.GetValues("sEcho")[0].ToString();
            // This query is required because the earlier query returns results for only 1 page.
            r.iTotalRecords = core.Messages.Where(m => (m.BusinessUnitId == BUId && m.PluginId == PluginId && m.Subject.Contains(strSearch) && m.IsDroppedYN == "N")).Count();
            r.iTotalDisplayRecords = r.iTotalRecords;
            foreach (Core.Models.Message msg in list)
            {
                List<string> r1 = new List<string>();
                string detailsurl = String.Format("<a href=\"/{0}/{1}/{2}\" id=\"detailslink\" title=\"{1}\" >Details</a>", "ContactUs/Admin", "Details", msg.MessageId.ToString());
                string deleteurl = String.Format("<a href=\"/{0}/{1}/{2}\" id=\"deletelink\" title=\"{1}\" >Delete</a>", "ContactUs/Admin", "Delete", msg.MessageId.ToString());
                r1.Add(detailsurl + deleteurl);
                r1.Add(msg.Contact.FirstName);
                r1.Add(msg.Contact.LastName);
                r1.Add(msg.Contact.Email);
                r1.Add(msg.Subject);
                r1.Add(String.Format("{0:g}", msg.CreateDT));
                r.aaData.Add(r1);
            }
            return Json(r);
        }

        //
        // GET: /Details/

        public ActionResult Details(Guid id)
        {
            CoreEntities core = new CoreEntities();
            Core.Models.Message msg = core.Messages.First(m => m.MessageId == id);
            return View(msg);
        }

        //
        // GET: /Delete/

        public ActionResult Delete(Guid id)
        {
            CoreEntities core = new CoreEntities();
            Core.Models.Message msg = core.Messages.First(m => m.MessageId == id);
            return View(msg);
        }

        //
        // POST: /Delete/

        [HttpPost]
        public ActionResult Delete(Guid MessageId, FormCollection collection)
        {
            CoreEntities core = new CoreEntities();
            //Guid MessageId = new Guid(collection["MessageId"][0].ToString());
            Core.Models.Message msg = core.Messages.First(m => m.MessageId == MessageId);
            msg.IsDroppedYN = "Y";
            core.SaveChanges();
            return RedirectToAction("Index");
        }
    }
}
